package com.zx.Ding.controller;/**
 * Author:JasonZ
 * Data:2023/3/13
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.Ding.common.R;
import com.zx.Ding.entity.Category;
import com.zx.Ding.entity.Dish;
import com.zx.Ding.entity.Store;
import com.zx.Ding.service.CategoryService;
import com.zx.Ding.service.DishService;
import com.zx.Ding.service.StoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: Ding
 *
 * @description:
 *
 * @author: JasonZ
 *
 * @create: 2023-03-13 19:20
 **/
@Slf4j
@RestController
@RequestMapping("/store")
public class StoreController {
    @Autowired
    private StoreService storeService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;

    @GetMapping("/page")
    public R<Page>Page(int page, int pageSize){
        log.info("page={},pageSize={}",page,pageSize);
        //mpPlus分页构造器
        Page pageInfo = new Page<>(page, pageSize);
        /*//把数据录入storeList当中
        ArrayList<Store> stores = new ArrayList<>();
        //1.获得所有的商铺List(店铺id 店铺名 菜品数量和销售量)
        LambdaQueryWrapper<Category> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Category::getType,1);
        List<Category> list = categoryService.list(queryWrapper1);
        list.forEach(s->{
            LambdaQueryWrapper<Dish> queryWrapper2 = new LambdaQueryWrapper<>();
            QueryWrapper<Dish> queryWrapper3 = new QueryWrapper<>();
            Store store = new Store(s.getName(),s.getId());
            queryWrapper2.eq(s.getId()!=null,Dish::getCategoryId,s.getId());
            int count1 = dishService.count(queryWrapper2);
            store.setTotalNums(count1);
            queryWrapper3.eq("category_id",s.getId());
            queryWrapper3.select("SUM(total_sal) as totalSals");
            Dish dish = dishService.getOne(queryWrapper3);
            store.setTotalSals(dish.getTotalSals());
            stores.add(store);
        });
        storeService.saveBatch(stores);*/
        //条件构造器
        LambdaQueryWrapper<Store> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Store::getTotalSals);
        //分页查询
        storeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    @GetMapping("/{categoryId}")
    public R<List<Dish> >get(@PathVariable Long categoryId){
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId!=null,Dish::getCategoryId,categoryId);
        queryWrapper.orderByDesc(Dish::getTotalSal);
        List<Dish> list = dishService.list(queryWrapper);
        return R.success(list);
    }
}